package 算法;

import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 字母暗号
 */
public class LetterSignal {


    public static void main(String[] args) throws InterruptedException {
        LinkedBlockingQueue<String> one = new LinkedBlockingQueue(9);
        for (char c : "ABCDEFGHI".toCharArray()) {
            one.put(String.valueOf(c));
        }

        LinkedBlockingQueue<String> two = new LinkedBlockingQueue(9);
        for (char c : "JKLMNOPQR".toCharArray()) {
            two.put(String.valueOf(c));
        }
        LinkedBlockingQueue<String> three = new LinkedBlockingQueue(9);

        for (char c : "STUVWXYZ*".toCharArray()) {
            three.put(String.valueOf(c));
        }
        LinkedBlockingQueue<LinkedBlockingQueue<String>> query = new LinkedBlockingQueue<>(3);
        query.put(one);
        query.put(two);
        query.put(three);


        Scanner scanner = new Scanner(System.in);
        String firstLine = scanner.nextLine().toLowerCase();
        String secendLine = scanner.nextLine().toUpperCase();


        //获取位移后的每个字母游标
        String[] split = firstLine.split(" ");
        int mon = (Integer.valueOf(split[0]) - 1) % 3; //月
        int day = (Integer.valueOf(split[1]) - 1) % 9; //日
        move(mon, query);
        for (LinkedBlockingQueue<String> e : query) {
            move(day, e);
        }
        Map<String, String> coordinate = coordinate(query);


        //遍历第二行， 寻找游标输出
        String tempn = secendLine.replaceAll("\\s", "*"); //将输入的字符串中的空格转换成*
        StringBuilder out = new StringBuilder();
        for (char c : tempn.toCharArray()) {
            String s = coordinate.get(String.valueOf(c));
            out.append(s).append(" ");
        }

        System.out.println(out.toString());
    }


    /**
     * 构建每个字母的游标
     *
     * @param querys
     * @return
     * @throws InterruptedException
     */
    private static Map<String, String> coordinate(LinkedBlockingQueue<LinkedBlockingQueue<String>> querys) throws InterruptedException {
        Map<String, String> map = new HashMap<>(32);
        for (int i = 1; i <= 3; i++) {
            LinkedBlockingQueue<String> take = querys.poll();
            for (int j = 1; j <= 9; j++) {
                String take1 = take.poll();
                map.put(take1, "" + i + j);
            }

        }
        return map;

    }


    /**
     * FIFO 位移
     *
     * @param num
     * @param queue
     * @throws InterruptedException
     */
    static void move(int num, LinkedBlockingQueue queue) throws InterruptedException {
        for (int i = 0; i < num; i++) {
            Object take = queue.poll();
            queue.put(take);
        }
    }


}

